package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        // 1.补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        System.out.println("createID = "+dishDto.getCreateUser());
        dishService.save(dishDto);
        return R.success("添加成功！");
    }

    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer pageSize,String name){
        Page<DishDto> pageResult = dishService.findByPage(page,pageSize,name);
        return R.success(pageResult);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);

    }

    @PutMapping
    public R updateDish(@RequestBody DishDto dishDto,HttpSession session){
        // 1.补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        //
        dishService.updateDish(dishDto);

        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishService.list(categoryId, status);
        return R.success(dishDtoList);
    }
}
